<?php

/**
 * Report_Jasper File
 * 
 * Short file description
 *
 * PHP Version: 5.3.26
 * 
 * @category Report_Jasper
 * @package  Report_Jasper_Package
 * @author   <johari@3fresources.com>  
 */

// put your namespace and use here

/**
 * Report_Jasper class
 * 
 * Class description
 *
 * @category Report_Jasper_Controller
 * @package  Report_Jasper_Package
 * @author   <johari@3fresources.com> 
 */

class Report_Jasper_Controller extends Base_Controller 
{

	public $restful = true;

     /**
	  * action_index Method
	  *
	  * @param string 
	  * @param int    
	  *
	  * @return string
	  */

	public function get_index()
	{
		if(URI::segment(5) == 'debug'):
			$reports = Reporter::viewFolder();

			echo "<pre>";
			print_r($reports);
		else:
			return Redirect::to('report/jasper/integrate');
		endif;

		
	}


     /**
	  * get_integrate Method
	  *
	  * populate report for webTAS  
	  *
	  * @return void
	  */

	public function get_integrate()
	{
		$reports = Reporter::viewFolder();
		$data['reports'] = $reports;

		$data['profile'] = $profile = Console_User::find(Auth::user()->su_id)->profile;
		$data['kumpulan'] = Data_Attendance_Group::arrayGroup();
		$data['jabatan'] = Data_Department_Department::arrayDept();
		$data['bahagian'] = ($profile)?Data_Department_Division::arrayDiv($profile->wpj_no_auto):array('Sila Pilih');
		$data['unit'] = ($profile)?Data_Department_Unit::arrayUnit($profile->wpj_no_auto,$profile->wpb_no_auto):array('Sila Pilih');

		$data['listID'] = Console_Profile::getStaffIDArray(Auth::user()->su_id);

		$data['preview'] = '';

		return View::make('report.jasper.integrate',$data);
	}


     /**
	  * get_fetch Method
	  *
	  * fetch report from jaspersoft   
	  *
	  * @return void
	  */

	public function get_fetch()
	{
		$input = Input::get();

		$oper = $input['oper'];
		$type = $input['ctype'];
		$rPort = $input['report'];

		unset($input['oper']);
		unset($input['ctype']);
		unset($input['type']);
		unset($input['report']);

		$params = $input;

		$client = new Reporter();
	    $report = $client->getReport($client->rootFolder.$client->folder. $rPort, $type,$params);

    	if($type == 'pdf'):
    		$type = 'application/'.$type;
    	endif;

	    if($oper == 'print' || $type == 'xls'):

	      header("Content-type: ".$type);
	      header( "Content-length: ".strlen($report)."");
	      header( "Content-Disposition: attachment; filename=".$rPort);
	      echo $report;

	    else:

	       header("Content-type: ".$type);
	       echo $report;

	    endif;

	    exit();
	}


}
